package com.csot.mybatis_plus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.csot.mybatis_plus.dao.CategoryMapper;
import com.csot.mybatis_plus.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sun.chuanwang
 * @version 1.0.0
 * @date 2021/1/17 22:10
 */
@Service
public class CategoryService  {
    @Autowired
    private CategoryMapper categoryMapper;
    /*@Autowired
    private IService<Category> iService;*/

    public List<Category> queryAll(){
        List<Category> categories = categoryMapper.selectList(null);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        //组装子级菜单


        /*List<Category> categoryList = categories.stream()
                .filter(category -> category.getParentId() == 0
                )*//*.map((menu)->{
                    menu.setChildren(getChildrens(menu,categories));
                    return menu;
                })*//*
                .collect(Collectors.toList());*/

        return categories;
    }

    private List<Category> getChildrens(Category root,List<Category> all){
        List<Category> collect = all.stream().filter(category -> category.getParentId() == root.getId())
                .map(category -> {
                    category.setChildren(getChildrens(category, all));
                    return category;
                }).collect(Collectors.toList());

        return collect;
    }

}
